package com.example.tnot.Teacher.CoursePart.CourseInternal.ClassDetail.VpFragment.AddChapter;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.tnot.R;
import com.example.tnot.Teacher.CoursePart.Container.AddCourse.Adapter.SelectTermAdapter;
import com.example.tnot.Teacher.CoursePart.Container.AddCourse.ItemBean.Term;
import com.example.tnot.Teacher.CoursePart.CourseInternal.ClassDetail.VpFragment.AddChapter.Adapter.SelectClassAdapter;
import com.example.tnot.Teacher.CoursePart.CourseInternal.ClassDetail.VpFragment.AddChapter.ItemBean.ClassItem;

import java.util.ArrayList;
import java.util.List;

public class SelectClassFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private View view;
    private List<ClassItem> classList;
    private ImageView back;
    private TextView tv_finish;
    RecyclerView rv_selectClass;
    NavController navController;
    public SelectClassFragment() {
    }

    public static SelectClassFragment newInstance(String param1, String param2) {
        SelectClassFragment fragment = new SelectClassFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.course_select_class, container, false);
        classList = new ArrayList<>();
        InitData();
        rv_selectClass = view.findViewById(R.id.rv_selectClass);
        rv_selectClass.setLayoutManager(new LinearLayoutManager(getContext()));
        SelectClassAdapter adapter = new SelectClassAdapter(classList);
        rv_selectClass.setAdapter(adapter);


        back = view.findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavController navController = Navigation.findNavController(v);
                navController.navigateUp();
            }
        });

        tv_finish = view.findViewById(R.id.tv_finish);
        tv_finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String className = adapter.getSelectedClass();

                if(className != null) {
                    try {
                        navController = Navigation.findNavController(v);
                        if (navController.getPreviousBackStackEntry() != null) {
                            navController.getPreviousBackStackEntry().getSavedStateHandle().set("selectedClass", className);
                        }
                    } catch (Exception e) {
                        Log.e("SelectClassFragment", "Error setting selected class", e);
                    }
                }
                navController.navigateUp();
            }
        });

        return view;
    }

    private void InitData() {
        classList.add(new ClassItem("数科2301"));
        classList.add(new ClassItem("数科2302"));
        classList.add(new ClassItem("数科2303"));

    }
}